package com.itheima.controller;

import com.itheima.pojo.User;
import org.springframework.web.bind.annotation.*;

/**
 * Description:
 *
 * @author codermao
 * @date 2023/11/7 18:43
 *
 *  TODO：restful的SpringMVC代码实现
 *
 *      1.用请求方式来区分不同操作
 *      2.请求参数
 *          1）简单参数直接放在地址中 @PathVariable
 *          2）如果是pojo参数，还是用json格式传递 @RequestBody
 */
//@Controller
public class RestController {
    /**
     * 前端请求地址：http://localhost:8080/rest
     * @return
     */
    @RequestMapping(value = "/rest",method = RequestMethod.GET,produces ="text/html;charset=utf-8")
    @ResponseBody
    public String getAll(){
        return "get：查询";
    }

    /**
     * 前端请求地址：http://localhost:8080/rest/3
     * 后端：
     *    @RequestMapping(value = "/rest/{id}",...) -- > id = 3
     *   @PathVariable Integer id   --> id = 3
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/rest/{id}",method = RequestMethod.GET,produces ="text/html;charset=utf-8")
    @ResponseBody
    public String getById(@PathVariable Integer id){
        System.out.println("getById：查询" + id);
        return "getById：根据id查询";
    }

    /**
     * 前端请求地址：http://localhost:8080/rest/{page}/{size}
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/rest/{page}/{size}",method = RequestMethod.GET,produces ="text/html;charset=utf-8")
    @ResponseBody
    public String getByPage(@PathVariable Integer page,@PathVariable Integer size){
        System.out.println("getByPage：查询" + page + "," + size);
        return "getByPage：分页查询";
    }

    @RequestMapping(value = "/rest",method = RequestMethod.POST,produces ="text/html;charset=utf-8")
    @ResponseBody
    public String post(@RequestBody User user){
        System.out.println("post：新增" + user);
        return "post：新增";
    }

    @RequestMapping(value = "/rest",method = RequestMethod.PUT,produces ="text/html;charset=utf-8")
    @ResponseBody
    public String put(@RequestBody User user){
        System.out.println("put修改：" + user);
        return "put：修改";
    }

    @RequestMapping(value = "/rest",method = RequestMethod.DELETE,produces ="text/html;charset=utf-8")
    @ResponseBody
    public String delete(){
        return "delete：删除";
    }

    @RequestMapping(value = "/rest/{id}",method = RequestMethod.DELETE,produces ="text/html;charset=utf-8")
    @ResponseBody
    public String delete2(@PathVariable Integer id){
        System.out.println("删除id：" + id);
        return "delete：删除";
    }
}
